package com.kg.rails.ui.panels.property;

import com.kg.rails.component.editor.model.custom.ElementCell;
import com.kg.rails.component.editor.swing.GraphComponent;

import javax.swing.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;

/**
 * User: Babanin
 * Date: 05.05.12
 * Time: 1:50
 */
public class ElementPropertyPanel extends PropertyPanel<ElementCell> {

    private static final NumberFormat DOUBLE_FORMATTER = new DecimalFormat("####,##");

    private JTextField lengthTextField;
    private JCheckBox directionCheckBox;

    public ElementPropertyPanel(ElementCell cell, GraphComponent component) {
        super(cell, component);

        lengthTextField = new JTextField();
        directionCheckBox = new JCheckBox();

        addProperty("Длина (км): ", lengthTextField);
        addProperty("Направление: ", directionCheckBox);
    }

    @Override
    protected void save() {
        try {
            cell.setLength(DOUBLE_FORMATTER.parse(lengthTextField.getText().trim()).doubleValue());
        } catch (ParseException e) {
            JOptionPane.showMessageDialog(null, "Длина блок-участка указана некорректно", "Ошибка", JOptionPane.ERROR_MESSAGE);
            return;
        }

        cell.setDirection(directionCheckBox.isSelected());

        super.save();
    }

    @Override
    protected void reset() {
        directionCheckBox.setSelected(cell.isDirection());
        lengthTextField.setText(DOUBLE_FORMATTER.format(cell.getLength()));

        super.reset();
    }

    @Override
    public String getDescription() {
        return "Свойства БУ";
    }
}
